﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BatallaNavalgo;

namespace InterfazGrafica
{
    public partial class Vista : Form
    {
        private Controlador controlador;

        public Vista()
        {
            InitializeComponent();
            this.DisparoConvencional.Visible = false;
            this.MinaPuntual.Visible = false;
            this.MinaDoble.Visible = false;
            this.MinaTriple.Visible = false;
            this.MinaPorContacto.Visible = false;
            this.pictureBox1.MouseClick += this.pictureBox1_MouseClick;
            this.pictureBox1.BackColor = Color.Transparent;
        }

        public Button Button1
        {
            get { return this.button1; }
            set { this.button1 = value; }
        }

        public Label Label1
        {
            get { return this.label1; }
            set { this.label1 = value; }
        }


        private void pictureBox1_Paint(object sender,EventArgs e, bool boolean,Tuple<int,int> posicion_inicial)
        {
            /*if (boolean)
            {
                Graphics g = this.pictureBox1.CreateGraphics();
                g.Clear(Color.Blue);
            }*/
            this.controlador.pictureBox1_Paint(sender, e,boolean,posicion_inicial);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.HacerClickEnButton1(sender, e);        
        }

        private bool OpcionesDeDisparoNoEstanVisibles()
        {
            if (this.DisparoConvencional.Visible == false && this.MinaPuntual.Visible == false && this.MinaDoble.Visible == false && MinaTriple.Visible == false && MinaPorContacto.Visible == false)
                return true;
            return false;
        }


        private void pictureBox1_MouseClick(Object sender, MouseEventArgs e)
        {
            this.button1.Visible = false;
            if (this.OpcionesDeDisparoNoEstanVisibles() && this.button1.Enabled == false)
            {
                this.controlador.RealizarTurno(sender, e,this.controlador.pictureBox1_Click(e));
                this.button2.Visible = true;
            }
        }

        
        private void CambiarEstadoDespuesDeCheckedChanged(RadioButton rb)
        {
            rb.Checked = false;
            this.DisparoConvencional.Visible = false;
            this.MinaPuntual.Visible = false;
            this.MinaDoble.Visible = false;
            this.MinaTriple.Visible = false;
            this.MinaPorContacto.Visible = false;
            this.button1.Visible = true;
            this.button1.Enabled = false;
            this.button1.Text = "SELECCIONE LA UBICACIÓN DENTRO DEL TABLERO";
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.controlador.RadioButton_CheckedChanged(1);
            this.CambiarEstadoDespuesDeCheckedChanged(this.DisparoConvencional);
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.controlador.RadioButton_CheckedChanged(2);
            this.CambiarEstadoDespuesDeCheckedChanged(this.MinaPuntual);
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            this.controlador.RadioButton_CheckedChanged(3);
            this.CambiarEstadoDespuesDeCheckedChanged(this.MinaDoble);
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            this.controlador.RadioButton_CheckedChanged(4);
            this.CambiarEstadoDespuesDeCheckedChanged(this.MinaTriple);
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            this.controlador.RadioButton_CheckedChanged(5);
            this.CambiarEstadoDespuesDeCheckedChanged(this.MinaPorContacto);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button1.Visible = false;
            this.controlador.RealizarTurno(sender,e,new Tuple<int,int>(0,0));
        }

        public void DibujarTablero(object sender, EventArgs e, bool boolean,Tuple<int,int> posicion_inicial)
        {
            this.pictureBox1_Paint(sender, e,boolean,posicion_inicial);
        }

        public void HacerClickEnButton1(Object sender, EventArgs e)
        {
            this.button2.Visible = false;
            if (this.controlador == null)
                this.controlador = new Controlador(this,0, 0, 40);
            if (this.controlador.button1_Click() == true)
            {
                this.pictureBox1_Paint(sender, e,true,new Tuple<int,int>(0,0));
                toolStripStatusLabel1.Text = "ESTADO ACTUAL";
                this.button2.Enabled = true;
                this.button2.Visible = true;
            }
        }

        public void DeshabilitarButton2()
        {
            this.button2.Enabled = false;
            this.button2.Visible = false;
        }

        
        public void RellenarCuadro(Color color, Rectangle rect)
        {
            SolidBrush solidBrush = new SolidBrush(color);
            Graphics g = this.pictureBox1.CreateGraphics();
            g.FillRectangle(solidBrush, rect);
            g.Dispose();
        }

        public void DibujarCuadro(Rectangle rect)
        {
            SolidBrush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(brush);
            Graphics g = this.pictureBox1.CreateGraphics();
            g.DrawRectangle(pen, rect);
            g.Dispose();
        }

        public void InsertarBarco(Icon icon, Rectangle rect)
        {
            Graphics g = this.pictureBox1.CreateGraphics();
            g.DrawIcon(icon, rect.X, rect.Y);
            g.Dispose();
        }

        public void HabilitarOpcionesDeDisparo()
        {
            this.DisparoConvencional.Visible = true;
            this.MinaPuntual.Visible = true;
            this.MinaDoble.Visible = true;
            this.MinaTriple.Visible = true;
            this.MinaPorContacto.Visible = true;
        }

        public void CargarBarco(int codigo,Rectangle rect)
        {
            String[] dibujos = new String[5] { "buque.ico", "destructor.ico", "lancha.ico", "portaaviones.ico", "rompehielos.ico"};
            Icon icon = new Icon (dibujos[codigo]);
            this.InsertarBarco(icon,rect);
        }

        public void Explotar(Rectangle rect)
        {
            String[] explosiones = new String[16] { "bomba0.ico", "bomba1.ico", "bomba2.ico", "bomba3.ico", "bomba4.ico", "bomba5.ico", "bomba6.ico", "bomba7.ico", "bomba8.ico", "bomba9.ico", "bomba10.ico", "bomba11.ico", "bomba12.ico", "bomba13.ico", "bomba14.ico", "bomba15.ico" };

            foreach (String foto in explosiones)
            {
                Icon icon = new Icon(foto);
                Graphics g = this.pictureBox1.CreateGraphics();
                g.DrawIcon(icon, rect.X, rect.Y);
                g.Dispose();
                System.Threading.Thread.Sleep(3);


            }

        }




        
        
    }
}
